package com.jlnk.androidfmc.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.entity.FavoriteItem;
import java.util.List;

public class FavoritesAdapter extends RecyclerView.Adapter<FavoritesAdapter.ViewHolder> {

    private List<FavoriteItem> favorites;
    private OnItemEditListener onItemEditListener;
    private OnItemDeleteListener onItemDeleteListener;

    public FavoritesAdapter(List<FavoriteItem> favorites) {
        this.favorites = favorites;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_favorite, parent, false);
        // 确保这里传递了正确的监听器实例
        return new ViewHolder(view, onItemEditListener, onItemDeleteListener);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (favorites == null) {
            // 处理 favorites 为 null 的情况，例如返回或者抛出异常
            return;
        }
        FavoriteItem item = favorites.get(position);
        holder.imageViewItem.setImageResource(item.getImageResourceId());
        holder.textViewName.setText(item.getName());
        holder.textViewPrice.setText(String.format("¥%.2f元/斤", item.getPrice()));

        // 设置点击监听器
        holder.editButton.setOnClickListener(v -> {
            if (onItemEditListener != null) {
                onItemEditListener.onEdit(holder.getAdapterPosition());
            }
        });
        holder.deleteButton.setOnClickListener(v -> {
            if (onItemDeleteListener != null) {
                onItemDeleteListener.onDelete(holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return favorites != null ? favorites.size() : 0;
    }


    public interface OnItemEditListener {
        void onEdit(int position);
    }

    public interface OnItemDeleteListener {
        void onDelete(int position);
    }


    public void setOnItemEditListener(OnItemEditListener listener) {
        this.onItemEditListener = listener;
    }

    public void setOnItemDeleteListener(OnItemDeleteListener listener) {
        this.onItemDeleteListener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageViewItem;
        TextView textViewName;
        TextView textViewPrice;
        Button editButton;
        Button deleteButton;

        public ViewHolder(@NonNull View itemView, OnItemEditListener editListener, OnItemDeleteListener deleteListener) {
            super(itemView);
            imageViewItem = itemView.findViewById(R.id.imageViewItem); // 只需要调用一次
            textViewName = itemView.findViewById(R.id.textViewName);
            textViewPrice = itemView.findViewById(R.id.textViewPrice);
            editButton = itemView.findViewById(R.id.editButton);
            deleteButton = itemView.findViewById(R.id.deleteButton);

            // 移除重复的设置监听器的代码，因为已经在 onBindViewHolder 中设置了
        }
    }
}